import {defineComponent, h, ref, reactive, watch, toRefs, onMounted,onUnmounted} from '@vue/runtime-core'
import  enemyPlaneImg from '../assets/enemy.png'


export const enemyPlaneInfo = {
    width: 308,
    height: 207,
    dir:1,
    life: 3
}
export default defineComponent({
    props:["x","y"],
    setup(props,ctx){
        const x = ref(props.x);
        const y = ref(props.y);
        //const {x,y} = toRefs(props)

        watch(props,(newVal)=>{
            x.value = newVal.x
            y.value = newVal.y
        })
        useAttack(ctx,x,y)
        return{
            X:x,
            Y:y
        }
    },
    render(ctx){
        //背景图片
        return h('Sprite',{x:ctx.X,y:ctx.Y,texture:enemyPlaneImg})
    }
});

const useAttack =(ctx,x,y)=>{
    let intervalId;
    onMounted(()=>{
        intervalId = setInterval(()=>{
            ctx.emit('attack',{x:x.value+105,y:y.value+200})
        },2000)
    })
    onUnmounted(()=>{
        clearInterval(intervalId)
    })
}